package run.halo.app.service.impl;

import org.springframework.stereotype.Service;
import run.halo.app.model.entity.Erfav;
import run.halo.app.model.entity.Post;
import run.halo.app.repository.ErfavRepository;
import run.halo.app.service.ErfavService;
import run.halo.app.service.PostService;
import run.halo.app.service.UserService;
import run.halo.app.service.base.AbstractCrudService;

@Service
public class ErfavServiceImpl extends AbstractCrudService<Erfav, Integer> implements ErfavService {
    private final ErfavRepository erfavRepository;
    private final UserService userService;
    private final PostService postService;

    public ErfavServiceImpl(ErfavRepository erfavRepository, UserService userService,PostService postService) {
        super(erfavRepository);
        this.erfavRepository=erfavRepository;
        this.userService=userService;
        this.postService=postService;
    }

    @Override
    public void favPost(Integer postId, String token) {
        Integer uid=userService.getUidFromToken(token);
        if (uid>0) {
            Erfav erfav = new Erfav();
            erfav.setUid(uid);
            erfav.setPostid(postId);
            Post post=postService.getById(postId);
            erfav.setFid(post.getUid());
            erfav.setFav(1);
            super.create(erfav);
        }
    }
}
